{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "metallic-tutorial",
   "metadata": {},
   "source": [
    "# 习题 5.4\n",
    "\n",
    "![习题 5.4](https://tva1.sinaimg.cn/large/008i3skNly1grbv1zgy77j316008ejut.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "searching-labor",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 35 × 8</caption>\n",
       "<thead>\n",
       "\t<tr><th scope=col>group</th><th scope=col>x1</th><th scope=col>x2</th><th scope=col>x3</th><th scope=col>x4</th><th scope=col>x5</th><th scope=col>x6</th><th scope=col>x7</th></tr>\n",
       "\t<tr><th scope=col>&lt;int&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><td>1</td><td>6.6</td><td> 39</td><td>1.0</td><td>6.0</td><td> 6</td><td>0.12</td><td> 20</td></tr>\n",
       "\t<tr><td>1</td><td>6.6</td><td> 39</td><td>1.0</td><td>6.0</td><td>12</td><td>0.12</td><td> 20</td></tr>\n",
       "\t<tr><td>1</td><td>6.1</td><td> 47</td><td>1.0</td><td>6.0</td><td> 6</td><td>0.08</td><td> 12</td></tr>\n",
       "\t<tr><td>1</td><td>6.1</td><td> 47</td><td>1.0</td><td>6.0</td><td>12</td><td>0.08</td><td> 12</td></tr>\n",
       "\t<tr><td>1</td><td>8.4</td><td> 32</td><td>2.0</td><td>7.5</td><td>19</td><td>0.35</td><td> 75</td></tr>\n",
       "\t<tr><td>1</td><td>7.2</td><td>  6</td><td>1.0</td><td>7.0</td><td>28</td><td>0.30</td><td> 30</td></tr>\n",
       "\t<tr><td>1</td><td>8.4</td><td>113</td><td>3.5</td><td>6.0</td><td>18</td><td>0.15</td><td> 75</td></tr>\n",
       "\t<tr><td>1</td><td>7.5</td><td> 52</td><td>1.0</td><td>6.0</td><td>12</td><td>0.16</td><td> 40</td></tr>\n",
       "\t<tr><td>1</td><td>7.5</td><td> 52</td><td>3.5</td><td>7.5</td><td> 6</td><td>0.16</td><td> 40</td></tr>\n",
       "\t<tr><td>1</td><td>8.3</td><td>113</td><td>0.0</td><td>7.5</td><td>35</td><td>0.12</td><td>180</td></tr>\n",
       "\t<tr><td>1</td><td>7.8</td><td>172</td><td>1.0</td><td>3.5</td><td>14</td><td>0.21</td><td> 45</td></tr>\n",
       "\t<tr><td>1</td><td>7.8</td><td>172</td><td>1.5</td><td>3.0</td><td>15</td><td>0.21</td><td> 45</td></tr>\n",
       "\t<tr><td>2</td><td>8.4</td><td> 32</td><td>1.0</td><td>5.0</td><td> 4</td><td>0.35</td><td> 75</td></tr>\n",
       "\t<tr><td>2</td><td>8.4</td><td> 32</td><td>2.0</td><td>9.0</td><td>10</td><td>0.35</td><td> 75</td></tr>\n",
       "\t<tr><td>2</td><td>8.4</td><td> 32</td><td>2.5</td><td>4.0</td><td>10</td><td>0.35</td><td> 75</td></tr>\n",
       "\t<tr><td>2</td><td>6.3</td><td> 11</td><td>4.5</td><td>7.5</td><td> 3</td><td>0.20</td><td> 15</td></tr>\n",
       "\t<tr><td>2</td><td>7.0</td><td>  8</td><td>4.5</td><td>4.5</td><td> 9</td><td>0.25</td><td> 30</td></tr>\n",
       "\t<tr><td>2</td><td>7.0</td><td>  8</td><td>6.0</td><td>7.5</td><td> 4</td><td>0.25</td><td> 30</td></tr>\n",
       "\t<tr><td>2</td><td>7.0</td><td>  8</td><td>1.5</td><td>6.0</td><td> 1</td><td>0.25</td><td> 30</td></tr>\n",
       "\t<tr><td>2</td><td>8.3</td><td>161</td><td>1.5</td><td>4.0</td><td> 4</td><td>0.08</td><td> 70</td></tr>\n",
       "\t<tr><td>2</td><td>8.3</td><td>161</td><td>0.5</td><td>2.5</td><td> 1</td><td>0.08</td><td> 70</td></tr>\n",
       "\t<tr><td>2</td><td>7.2</td><td>  6</td><td>3.5</td><td>4.0</td><td>12</td><td>0.30</td><td> 30</td></tr>\n",
       "\t<tr><td>2</td><td>7.2</td><td>  6</td><td>1.0</td><td>3.0</td><td> 3</td><td>0.30</td><td> 30</td></tr>\n",
       "\t<tr><td>2</td><td>7.2</td><td>  6</td><td>1.0</td><td>6.0</td><td> 5</td><td>0.30</td><td> 30</td></tr>\n",
       "\t<tr><td>2</td><td>5.5</td><td>  6</td><td>2.5</td><td>3.0</td><td> 7</td><td>0.18</td><td> 18</td></tr>\n",
       "\t<tr><td>2</td><td>8.4</td><td>113</td><td>3.5</td><td>4.5</td><td> 6</td><td>0.15</td><td> 75</td></tr>\n",
       "\t<tr><td>2</td><td>8.4</td><td>113</td><td>3.5</td><td>4.5</td><td> 8</td><td>0.15</td><td> 75</td></tr>\n",
       "\t<tr><td>2</td><td>7.5</td><td> 52</td><td>1.0</td><td>6.0</td><td> 6</td><td>0.16</td><td> 40</td></tr>\n",
       "\t<tr><td>2</td><td>7.5</td><td> 52</td><td>1.0</td><td>7.5</td><td> 8</td><td>0.16</td><td> 40</td></tr>\n",
       "\t<tr><td>2</td><td>8.3</td><td> 97</td><td>0.0</td><td>6.0</td><td> 5</td><td>0.15</td><td>180</td></tr>\n",
       "\t<tr><td>2</td><td>8.3</td><td> 97</td><td>2.5</td><td>6.0</td><td> 5</td><td>0.15</td><td>180</td></tr>\n",
       "\t<tr><td>2</td><td>8.3</td><td> 89</td><td>0.0</td><td>6.0</td><td>10</td><td>0.16</td><td>180</td></tr>\n",
       "\t<tr><td>2</td><td>8.3</td><td> 56</td><td>1.5</td><td>6.0</td><td>13</td><td>0.25</td><td>180</td></tr>\n",
       "\t<tr><td>2</td><td>7.8</td><td>172</td><td>1.0</td><td>3.5</td><td> 6</td><td>0.21</td><td> 45</td></tr>\n",
       "\t<tr><td>2</td><td>7.8</td><td>233</td><td>1.0</td><td>4.5</td><td> 6</td><td>0.18</td><td> 45</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 35 × 8\n",
       "\\begin{tabular}{llllllll}\n",
       " group & x1 & x2 & x3 & x4 & x5 & x6 & x7\\\\\n",
       " <int> & <dbl> & <int> & <dbl> & <dbl> & <int> & <dbl> & <int>\\\\\n",
       "\\hline\n",
       "\t 1 & 6.6 &  39 & 1.0 & 6.0 &  6 & 0.12 &  20\\\\\n",
       "\t 1 & 6.6 &  39 & 1.0 & 6.0 & 12 & 0.12 &  20\\\\\n",
       "\t 1 & 6.1 &  47 & 1.0 & 6.0 &  6 & 0.08 &  12\\\\\n",
       "\t 1 & 6.1 &  47 & 1.0 & 6.0 & 12 & 0.08 &  12\\\\\n",
       "\t 1 & 8.4 &  32 & 2.0 & 7.5 & 19 & 0.35 &  75\\\\\n",
       "\t 1 & 7.2 &   6 & 1.0 & 7.0 & 28 & 0.30 &  30\\\\\n",
       "\t 1 & 8.4 & 113 & 3.5 & 6.0 & 18 & 0.15 &  75\\\\\n",
       "\t 1 & 7.5 &  52 & 1.0 & 6.0 & 12 & 0.16 &  40\\\\\n",
       "\t 1 & 7.5 &  52 & 3.5 & 7.5 &  6 & 0.16 &  40\\\\\n",
       "\t 1 & 8.3 & 113 & 0.0 & 7.5 & 35 & 0.12 & 180\\\\\n",
       "\t 1 & 7.8 & 172 & 1.0 & 3.5 & 14 & 0.21 &  45\\\\\n",
       "\t 1 & 7.8 & 172 & 1.5 & 3.0 & 15 & 0.21 &  45\\\\\n",
       "\t 2 & 8.4 &  32 & 1.0 & 5.0 &  4 & 0.35 &  75\\\\\n",
       "\t 2 & 8.4 &  32 & 2.0 & 9.0 & 10 & 0.35 &  75\\\\\n",
       "\t 2 & 8.4 &  32 & 2.5 & 4.0 & 10 & 0.35 &  75\\\\\n",
       "\t 2 & 6.3 &  11 & 4.5 & 7.5 &  3 & 0.20 &  15\\\\\n",
       "\t 2 & 7.0 &   8 & 4.5 & 4.5 &  9 & 0.25 &  30\\\\\n",
       "\t 2 & 7.0 &   8 & 6.0 & 7.5 &  4 & 0.25 &  30\\\\\n",
       "\t 2 & 7.0 &   8 & 1.5 & 6.0 &  1 & 0.25 &  30\\\\\n",
       "\t 2 & 8.3 & 161 & 1.5 & 4.0 &  4 & 0.08 &  70\\\\\n",
       "\t 2 & 8.3 & 161 & 0.5 & 2.5 &  1 & 0.08 &  70\\\\\n",
       "\t 2 & 7.2 &   6 & 3.5 & 4.0 & 12 & 0.30 &  30\\\\\n",
       "\t 2 & 7.2 &   6 & 1.0 & 3.0 &  3 & 0.30 &  30\\\\\n",
       "\t 2 & 7.2 &   6 & 1.0 & 6.0 &  5 & 0.30 &  30\\\\\n",
       "\t 2 & 5.5 &   6 & 2.5 & 3.0 &  7 & 0.18 &  18\\\\\n",
       "\t 2 & 8.4 & 113 & 3.5 & 4.5 &  6 & 0.15 &  75\\\\\n",
       "\t 2 & 8.4 & 113 & 3.5 & 4.5 &  8 & 0.15 &  75\\\\\n",
       "\t 2 & 7.5 &  52 & 1.0 & 6.0 &  6 & 0.16 &  40\\\\\n",
       "\t 2 & 7.5 &  52 & 1.0 & 7.5 &  8 & 0.16 &  40\\\\\n",
       "\t 2 & 8.3 &  97 & 0.0 & 6.0 &  5 & 0.15 & 180\\\\\n",
       "\t 2 & 8.3 &  97 & 2.5 & 6.0 &  5 & 0.15 & 180\\\\\n",
       "\t 2 & 8.3 &  89 & 0.0 & 6.0 & 10 & 0.16 & 180\\\\\n",
       "\t 2 & 8.3 &  56 & 1.5 & 6.0 & 13 & 0.25 & 180\\\\\n",
       "\t 2 & 7.8 & 172 & 1.0 & 3.5 &  6 & 0.21 &  45\\\\\n",
       "\t 2 & 7.8 & 233 & 1.0 & 4.5 &  6 & 0.18 &  45\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 35 × 8\n",
       "\n",
       "| group &lt;int&gt; | x1 &lt;dbl&gt; | x2 &lt;int&gt; | x3 &lt;dbl&gt; | x4 &lt;dbl&gt; | x5 &lt;int&gt; | x6 &lt;dbl&gt; | x7 &lt;int&gt; |\n",
       "|---|---|---|---|---|---|---|---|\n",
       "| 1 | 6.6 |  39 | 1.0 | 6.0 |  6 | 0.12 |  20 |\n",
       "| 1 | 6.6 |  39 | 1.0 | 6.0 | 12 | 0.12 |  20 |\n",
       "| 1 | 6.1 |  47 | 1.0 | 6.0 |  6 | 0.08 |  12 |\n",
       "| 1 | 6.1 |  47 | 1.0 | 6.0 | 12 | 0.08 |  12 |\n",
       "| 1 | 8.4 |  32 | 2.0 | 7.5 | 19 | 0.35 |  75 |\n",
       "| 1 | 7.2 |   6 | 1.0 | 7.0 | 28 | 0.30 |  30 |\n",
       "| 1 | 8.4 | 113 | 3.5 | 6.0 | 18 | 0.15 |  75 |\n",
       "| 1 | 7.5 |  52 | 1.0 | 6.0 | 12 | 0.16 |  40 |\n",
       "| 1 | 7.5 |  52 | 3.5 | 7.5 |  6 | 0.16 |  40 |\n",
       "| 1 | 8.3 | 113 | 0.0 | 7.5 | 35 | 0.12 | 180 |\n",
       "| 1 | 7.8 | 172 | 1.0 | 3.5 | 14 | 0.21 |  45 |\n",
       "| 1 | 7.8 | 172 | 1.5 | 3.0 | 15 | 0.21 |  45 |\n",
       "| 2 | 8.4 |  32 | 1.0 | 5.0 |  4 | 0.35 |  75 |\n",
       "| 2 | 8.4 |  32 | 2.0 | 9.0 | 10 | 0.35 |  75 |\n",
       "| 2 | 8.4 |  32 | 2.5 | 4.0 | 10 | 0.35 |  75 |\n",
       "| 2 | 6.3 |  11 | 4.5 | 7.5 |  3 | 0.20 |  15 |\n",
       "| 2 | 7.0 |   8 | 4.5 | 4.5 |  9 | 0.25 |  30 |\n",
       "| 2 | 7.0 |   8 | 6.0 | 7.5 |  4 | 0.25 |  30 |\n",
       "| 2 | 7.0 |   8 | 1.5 | 6.0 |  1 | 0.25 |  30 |\n",
       "| 2 | 8.3 | 161 | 1.5 | 4.0 |  4 | 0.08 |  70 |\n",
       "| 2 | 8.3 | 161 | 0.5 | 2.5 |  1 | 0.08 |  70 |\n",
       "| 2 | 7.2 |   6 | 3.5 | 4.0 | 12 | 0.30 |  30 |\n",
       "| 2 | 7.2 |   6 | 1.0 | 3.0 |  3 | 0.30 |  30 |\n",
       "| 2 | 7.2 |   6 | 1.0 | 6.0 |  5 | 0.30 |  30 |\n",
       "| 2 | 5.5 |   6 | 2.5 | 3.0 |  7 | 0.18 |  18 |\n",
       "| 2 | 8.4 | 113 | 3.5 | 4.5 |  6 | 0.15 |  75 |\n",
       "| 2 | 8.4 | 113 | 3.5 | 4.5 |  8 | 0.15 |  75 |\n",
       "| 2 | 7.5 |  52 | 1.0 | 6.0 |  6 | 0.16 |  40 |\n",
       "| 2 | 7.5 |  52 | 1.0 | 7.5 |  8 | 0.16 |  40 |\n",
       "| 2 | 8.3 |  97 | 0.0 | 6.0 |  5 | 0.15 | 180 |\n",
       "| 2 | 8.3 |  97 | 2.5 | 6.0 |  5 | 0.15 | 180 |\n",
       "| 2 | 8.3 |  89 | 0.0 | 6.0 | 10 | 0.16 | 180 |\n",
       "| 2 | 8.3 |  56 | 1.5 | 6.0 | 13 | 0.25 | 180 |\n",
       "| 2 | 7.8 | 172 | 1.0 | 3.5 |  6 | 0.21 |  45 |\n",
       "| 2 | 7.8 | 233 | 1.0 | 4.5 |  6 | 0.18 |  45 |\n",
       "\n"
      ],
      "text/plain": [
       "   group x1  x2  x3  x4  x5 x6   x7 \n",
       "1  1     6.6  39 1.0 6.0  6 0.12  20\n",
       "2  1     6.6  39 1.0 6.0 12 0.12  20\n",
       "3  1     6.1  47 1.0 6.0  6 0.08  12\n",
       "4  1     6.1  47 1.0 6.0 12 0.08  12\n",
       "5  1     8.4  32 2.0 7.5 19 0.35  75\n",
       "6  1     7.2   6 1.0 7.0 28 0.30  30\n",
       "7  1     8.4 113 3.5 6.0 18 0.15  75\n",
       "8  1     7.5  52 1.0 6.0 12 0.16  40\n",
       "9  1     7.5  52 3.5 7.5  6 0.16  40\n",
       "10 1     8.3 113 0.0 7.5 35 0.12 180\n",
       "11 1     7.8 172 1.0 3.5 14 0.21  45\n",
       "12 1     7.8 172 1.5 3.0 15 0.21  45\n",
       "13 2     8.4  32 1.0 5.0  4 0.35  75\n",
       "14 2     8.4  32 2.0 9.0 10 0.35  75\n",
       "15 2     8.4  32 2.5 4.0 10 0.35  75\n",
       "16 2     6.3  11 4.5 7.5  3 0.20  15\n",
       "17 2     7.0   8 4.5 4.5  9 0.25  30\n",
       "18 2     7.0   8 6.0 7.5  4 0.25  30\n",
       "19 2     7.0   8 1.5 6.0  1 0.25  30\n",
       "20 2     8.3 161 1.5 4.0  4 0.08  70\n",
       "21 2     8.3 161 0.5 2.5  1 0.08  70\n",
       "22 2     7.2   6 3.5 4.0 12 0.30  30\n",
       "23 2     7.2   6 1.0 3.0  3 0.30  30\n",
       "24 2     7.2   6 1.0 6.0  5 0.30  30\n",
       "25 2     5.5   6 2.5 3.0  7 0.18  18\n",
       "26 2     8.4 113 3.5 4.5  6 0.15  75\n",
       "27 2     8.4 113 3.5 4.5  8 0.15  75\n",
       "28 2     7.5  52 1.0 6.0  6 0.16  40\n",
       "29 2     7.5  52 1.0 7.5  8 0.16  40\n",
       "30 2     8.3  97 0.0 6.0  5 0.15 180\n",
       "31 2     8.3  97 2.5 6.0  5 0.15 180\n",
       "32 2     8.3  89 0.0 6.0 10 0.16 180\n",
       "33 2     8.3  56 1.5 6.0 13 0.25 180\n",
       "34 2     7.8 172 1.0 3.5  6 0.21  45\n",
       "35 2     7.8 233 1.0 4.5  6 0.18  45"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data <- read.csv(\"ex_5_4.csv\")[-1]\n",
    "attach(data)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "welcome-processing",
   "metadata": {},
   "source": [
    "通过 MASS 包的 lda 进行判别分析："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "suspected-director",
   "metadata": {},
   "outputs": [],
   "source": [
    "library(MASS)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "infectious-germany",
   "metadata": {},
   "source": [
    "## 先验概率按比例分配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "julian-jersey",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Call:\n",
       "lda(group ~ ., data = data)\n",
       "\n",
       "Prior probabilities of groups:\n",
       "        1         2 \n",
       "0.3428571 0.6571429 \n",
       "\n",
       "Group means:\n",
       "        x1       x2       x3      x4        x5        x6       x7\n",
       "1 7.358333 73.66667 1.458333 6.00000 15.250000 0.1716667 49.50000\n",
       "2 7.686957 67.43478 2.043478 5.23913  6.347826 0.2156522 70.34783\n",
       "\n",
       "Coefficients of linear discriminants:\n",
       "             LD1\n",
       "x1 -1.747293e-01\n",
       "x2  1.416021e-05\n",
       "x3  2.072826e-01\n",
       "x4 -2.052228e-01\n",
       "x5 -1.935168e-01\n",
       "x6  8.395266e+00\n",
       "x7  1.917129e-02"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "l = lda(group ~ ., data=data); l"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "extended-walker",
   "metadata": {},
   "source": [
    "输出结果中 (ref https://cloud.tencent.com/developer/article/1553504 )：\n",
    "\n",
    "- Call 表示调用方法；\n",
    "- Prior probabilities of groups 表示先验概率；\n",
    "- Group means 表示每一类样本的均值；\n",
    "- Coefficients of linear discriminants 表示线性判别系数；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "stopped-carroll",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W = -0.17472934*x_1 + 1.416e-05*x_2 + 0.20728259*x_3 + -0.20522277*x_4 + -0.1935168*x_5 + 8.395266*x_6 + 0.01917129*x_7 +"
     ]
    }
   ],
   "source": [
    "# 输出线性判别函数\n",
    "cat(\"W =\", paste(round(l$scaling, 8), \"*x_\", 1:7, \" +\", sep=\"\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "electric-gambling",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 35 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>class</th><th scope=col>posterior.1</th><th scope=col>posterior.2</th><th scope=col>LD1</th><th scope=col>data.group</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>1</td><td>0.6859662059</td><td>3.140338e-01</td><td>-0.9541788</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>2</th><td>1</td><td>0.9807303897</td><td>1.926961e-02</td><td>-2.1152796</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>3</th><td>1</td><td>0.8665260167</td><td>1.334740e-01</td><td>-1.3558819</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>4</th><td>1</td><td>0.9934324560</td><td>6.567544e-03</td><td>-2.5169826</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>5</th><td>1</td><td>0.6533234780</td><td>3.466765e-01</td><td>-0.8997285</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>6</th><td>1</td><td>0.9998065233</td><td>1.934767e-04</td><td>-3.8192153</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>7</th><td>1</td><td>0.9517049526</td><td>4.829505e-02</td><td>-1.7653599</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>8</th><td>1</td><td>0.9172384730</td><td>8.276153e-02</td><td>-1.5531155</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>9</th><td>2</td><td>0.2119047770</td><td>7.880952e-01</td><td>-0.1816424</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>10</th><td>1</td><td>0.9999488458</td><td>5.115424e-05</td><td>-4.3098682</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>11</th><td>1</td><td>0.6906278386</td><td>3.093722e-01</td><td>-0.9621920</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>12</th><td>1</td><td>0.6832009800</td><td>3.167990e-01</td><td>-0.9494561</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>13</th><td>2</td><td>0.0003137630</td><td>9.996862e-01</td><td> 2.3087978</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>14</th><td>2</td><td>0.0371741982</td><td>9.628258e-01</td><td> 0.5340885</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>15</th><td>2</td><td>0.0018008368</td><td>9.981992e-01</td><td> 1.6638437</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>16</th><td>2</td><td>0.0258888529</td><td>9.741111e-01</td><td> 0.6718136</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>17</th><td>2</td><td>0.0233176914</td><td>9.766823e-01</td><td> 0.7113608</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>18</th><td>2</td><td>0.0039412644</td><td>9.960587e-01</td><td> 1.3742004</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>19</th><td>2</td><td>0.0044431240</td><td>9.955569e-01</td><td> 1.3298132</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>20</th><td>2</td><td>0.0724237508</td><td>9.275762e-01</td><td> 0.2743832</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>21</th><td>2</td><td>0.0121654340</td><td>9.878346e-01</td><td> 0.9554851</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>22</th><td>2</td><td>0.0511593102</td><td>9.488407e-01</td><td> 0.4109283</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>23</th><td>2</td><td>0.0011189212</td><td>9.988811e-01</td><td> 1.8395958</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>24</th><td>2</td><td>0.0167026449</td><td>9.832974e-01</td><td> 0.8368939</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>25</th><td>2</td><td>0.0479575131</td><td>9.520425e-01</td><td> 0.4360049</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>26</th><td>2</td><td>0.0155093991</td><td>9.844906e-01</td><td> 0.8646758</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>27</th><td>2</td><td>0.0430578686</td><td>9.569421e-01</td><td> 0.4776422</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>28</th><td>2</td><td>0.3223405288</td><td>6.776595e-01</td><td>-0.3920147</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>29</th><td>1</td><td>0.7578938074</td><td>2.421062e-01</td><td>-1.0868825</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>30</th><td>2</td><td>0.0006240626</td><td>9.993759e-01</td><td> 2.0551013</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>31</th><td>2</td><td>0.0001531707</td><td>9.998468e-01</td><td> 2.5733078</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>32</th><td>2</td><td>0.0068117313</td><td>9.931883e-01</td><td> 1.1713567</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>33</th><td>2</td><td>0.0018353157</td><td>9.981647e-01</td><td> 1.6568369</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>34</th><td>2</td><td>0.0324563440</td><td>9.675437e-01</td><td> 0.5859424</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>35</th><td>2</td><td>0.1036058164</td><td>8.963942e-01</td><td> 0.1297254</td><td>2</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 35 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & class & posterior.1 & posterior.2 & LD1 & data.group\\\\\n",
       "  & <fct> & <dbl> & <dbl> & <dbl> & <int>\\\\\n",
       "\\hline\n",
       "\t1 & 1 & 0.6859662059 & 3.140338e-01 & -0.9541788 & 1\\\\\n",
       "\t2 & 1 & 0.9807303897 & 1.926961e-02 & -2.1152796 & 1\\\\\n",
       "\t3 & 1 & 0.8665260167 & 1.334740e-01 & -1.3558819 & 1\\\\\n",
       "\t4 & 1 & 0.9934324560 & 6.567544e-03 & -2.5169826 & 1\\\\\n",
       "\t5 & 1 & 0.6533234780 & 3.466765e-01 & -0.8997285 & 1\\\\\n",
       "\t6 & 1 & 0.9998065233 & 1.934767e-04 & -3.8192153 & 1\\\\\n",
       "\t7 & 1 & 0.9517049526 & 4.829505e-02 & -1.7653599 & 1\\\\\n",
       "\t8 & 1 & 0.9172384730 & 8.276153e-02 & -1.5531155 & 1\\\\\n",
       "\t9 & 2 & 0.2119047770 & 7.880952e-01 & -0.1816424 & 1\\\\\n",
       "\t10 & 1 & 0.9999488458 & 5.115424e-05 & -4.3098682 & 1\\\\\n",
       "\t11 & 1 & 0.6906278386 & 3.093722e-01 & -0.9621920 & 1\\\\\n",
       "\t12 & 1 & 0.6832009800 & 3.167990e-01 & -0.9494561 & 1\\\\\n",
       "\t13 & 2 & 0.0003137630 & 9.996862e-01 &  2.3087978 & 2\\\\\n",
       "\t14 & 2 & 0.0371741982 & 9.628258e-01 &  0.5340885 & 2\\\\\n",
       "\t15 & 2 & 0.0018008368 & 9.981992e-01 &  1.6638437 & 2\\\\\n",
       "\t16 & 2 & 0.0258888529 & 9.741111e-01 &  0.6718136 & 2\\\\\n",
       "\t17 & 2 & 0.0233176914 & 9.766823e-01 &  0.7113608 & 2\\\\\n",
       "\t18 & 2 & 0.0039412644 & 9.960587e-01 &  1.3742004 & 2\\\\\n",
       "\t19 & 2 & 0.0044431240 & 9.955569e-01 &  1.3298132 & 2\\\\\n",
       "\t20 & 2 & 0.0724237508 & 9.275762e-01 &  0.2743832 & 2\\\\\n",
       "\t21 & 2 & 0.0121654340 & 9.878346e-01 &  0.9554851 & 2\\\\\n",
       "\t22 & 2 & 0.0511593102 & 9.488407e-01 &  0.4109283 & 2\\\\\n",
       "\t23 & 2 & 0.0011189212 & 9.988811e-01 &  1.8395958 & 2\\\\\n",
       "\t24 & 2 & 0.0167026449 & 9.832974e-01 &  0.8368939 & 2\\\\\n",
       "\t25 & 2 & 0.0479575131 & 9.520425e-01 &  0.4360049 & 2\\\\\n",
       "\t26 & 2 & 0.0155093991 & 9.844906e-01 &  0.8646758 & 2\\\\\n",
       "\t27 & 2 & 0.0430578686 & 9.569421e-01 &  0.4776422 & 2\\\\\n",
       "\t28 & 2 & 0.3223405288 & 6.776595e-01 & -0.3920147 & 2\\\\\n",
       "\t29 & 1 & 0.7578938074 & 2.421062e-01 & -1.0868825 & 2\\\\\n",
       "\t30 & 2 & 0.0006240626 & 9.993759e-01 &  2.0551013 & 2\\\\\n",
       "\t31 & 2 & 0.0001531707 & 9.998468e-01 &  2.5733078 & 2\\\\\n",
       "\t32 & 2 & 0.0068117313 & 9.931883e-01 &  1.1713567 & 2\\\\\n",
       "\t33 & 2 & 0.0018353157 & 9.981647e-01 &  1.6568369 & 2\\\\\n",
       "\t34 & 2 & 0.0324563440 & 9.675437e-01 &  0.5859424 & 2\\\\\n",
       "\t35 & 2 & 0.1036058164 & 8.963942e-01 &  0.1297254 & 2\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 35 × 5\n",
       "\n",
       "| <!--/--> | class &lt;fct&gt; | posterior.1 &lt;dbl&gt; | posterior.2 &lt;dbl&gt; | LD1 &lt;dbl&gt; | data.group &lt;int&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 1 | 1 | 0.6859662059 | 3.140338e-01 | -0.9541788 | 1 |\n",
       "| 2 | 1 | 0.9807303897 | 1.926961e-02 | -2.1152796 | 1 |\n",
       "| 3 | 1 | 0.8665260167 | 1.334740e-01 | -1.3558819 | 1 |\n",
       "| 4 | 1 | 0.9934324560 | 6.567544e-03 | -2.5169826 | 1 |\n",
       "| 5 | 1 | 0.6533234780 | 3.466765e-01 | -0.8997285 | 1 |\n",
       "| 6 | 1 | 0.9998065233 | 1.934767e-04 | -3.8192153 | 1 |\n",
       "| 7 | 1 | 0.9517049526 | 4.829505e-02 | -1.7653599 | 1 |\n",
       "| 8 | 1 | 0.9172384730 | 8.276153e-02 | -1.5531155 | 1 |\n",
       "| 9 | 2 | 0.2119047770 | 7.880952e-01 | -0.1816424 | 1 |\n",
       "| 10 | 1 | 0.9999488458 | 5.115424e-05 | -4.3098682 | 1 |\n",
       "| 11 | 1 | 0.6906278386 | 3.093722e-01 | -0.9621920 | 1 |\n",
       "| 12 | 1 | 0.6832009800 | 3.167990e-01 | -0.9494561 | 1 |\n",
       "| 13 | 2 | 0.0003137630 | 9.996862e-01 |  2.3087978 | 2 |\n",
       "| 14 | 2 | 0.0371741982 | 9.628258e-01 |  0.5340885 | 2 |\n",
       "| 15 | 2 | 0.0018008368 | 9.981992e-01 |  1.6638437 | 2 |\n",
       "| 16 | 2 | 0.0258888529 | 9.741111e-01 |  0.6718136 | 2 |\n",
       "| 17 | 2 | 0.0233176914 | 9.766823e-01 |  0.7113608 | 2 |\n",
       "| 18 | 2 | 0.0039412644 | 9.960587e-01 |  1.3742004 | 2 |\n",
       "| 19 | 2 | 0.0044431240 | 9.955569e-01 |  1.3298132 | 2 |\n",
       "| 20 | 2 | 0.0724237508 | 9.275762e-01 |  0.2743832 | 2 |\n",
       "| 21 | 2 | 0.0121654340 | 9.878346e-01 |  0.9554851 | 2 |\n",
       "| 22 | 2 | 0.0511593102 | 9.488407e-01 |  0.4109283 | 2 |\n",
       "| 23 | 2 | 0.0011189212 | 9.988811e-01 |  1.8395958 | 2 |\n",
       "| 24 | 2 | 0.0167026449 | 9.832974e-01 |  0.8368939 | 2 |\n",
       "| 25 | 2 | 0.0479575131 | 9.520425e-01 |  0.4360049 | 2 |\n",
       "| 26 | 2 | 0.0155093991 | 9.844906e-01 |  0.8646758 | 2 |\n",
       "| 27 | 2 | 0.0430578686 | 9.569421e-01 |  0.4776422 | 2 |\n",
       "| 28 | 2 | 0.3223405288 | 6.776595e-01 | -0.3920147 | 2 |\n",
       "| 29 | 1 | 0.7578938074 | 2.421062e-01 | -1.0868825 | 2 |\n",
       "| 30 | 2 | 0.0006240626 | 9.993759e-01 |  2.0551013 | 2 |\n",
       "| 31 | 2 | 0.0001531707 | 9.998468e-01 |  2.5733078 | 2 |\n",
       "| 32 | 2 | 0.0068117313 | 9.931883e-01 |  1.1713567 | 2 |\n",
       "| 33 | 2 | 0.0018353157 | 9.981647e-01 |  1.6568369 | 2 |\n",
       "| 34 | 2 | 0.0324563440 | 9.675437e-01 |  0.5859424 | 2 |\n",
       "| 35 | 2 | 0.1036058164 | 8.963942e-01 |  0.1297254 | 2 |\n",
       "\n"
      ],
      "text/plain": [
       "   class posterior.1  posterior.2  LD1        data.group\n",
       "1  1     0.6859662059 3.140338e-01 -0.9541788 1         \n",
       "2  1     0.9807303897 1.926961e-02 -2.1152796 1         \n",
       "3  1     0.8665260167 1.334740e-01 -1.3558819 1         \n",
       "4  1     0.9934324560 6.567544e-03 -2.5169826 1         \n",
       "5  1     0.6533234780 3.466765e-01 -0.8997285 1         \n",
       "6  1     0.9998065233 1.934767e-04 -3.8192153 1         \n",
       "7  1     0.9517049526 4.829505e-02 -1.7653599 1         \n",
       "8  1     0.9172384730 8.276153e-02 -1.5531155 1         \n",
       "9  2     0.2119047770 7.880952e-01 -0.1816424 1         \n",
       "10 1     0.9999488458 5.115424e-05 -4.3098682 1         \n",
       "11 1     0.6906278386 3.093722e-01 -0.9621920 1         \n",
       "12 1     0.6832009800 3.167990e-01 -0.9494561 1         \n",
       "13 2     0.0003137630 9.996862e-01  2.3087978 2         \n",
       "14 2     0.0371741982 9.628258e-01  0.5340885 2         \n",
       "15 2     0.0018008368 9.981992e-01  1.6638437 2         \n",
       "16 2     0.0258888529 9.741111e-01  0.6718136 2         \n",
       "17 2     0.0233176914 9.766823e-01  0.7113608 2         \n",
       "18 2     0.0039412644 9.960587e-01  1.3742004 2         \n",
       "19 2     0.0044431240 9.955569e-01  1.3298132 2         \n",
       "20 2     0.0724237508 9.275762e-01  0.2743832 2         \n",
       "21 2     0.0121654340 9.878346e-01  0.9554851 2         \n",
       "22 2     0.0511593102 9.488407e-01  0.4109283 2         \n",
       "23 2     0.0011189212 9.988811e-01  1.8395958 2         \n",
       "24 2     0.0167026449 9.832974e-01  0.8368939 2         \n",
       "25 2     0.0479575131 9.520425e-01  0.4360049 2         \n",
       "26 2     0.0155093991 9.844906e-01  0.8646758 2         \n",
       "27 2     0.0430578686 9.569421e-01  0.4776422 2         \n",
       "28 2     0.3223405288 6.776595e-01 -0.3920147 2         \n",
       "29 1     0.7578938074 2.421062e-01 -1.0868825 2         \n",
       "30 2     0.0006240626 9.993759e-01  2.0551013 2         \n",
       "31 2     0.0001531707 9.998468e-01  2.5733078 2         \n",
       "32 2     0.0068117313 9.931883e-01  1.1713567 2         \n",
       "33 2     0.0018353157 9.981647e-01  1.6568369 2         \n",
       "34 2     0.0324563440 9.675437e-01  0.5859424 2         \n",
       "35 2     0.1036058164 8.963942e-01  0.1297254 2         "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pred <- predict(l)\n",
    "pred.tab <- data.frame(pred, data.group=group); pred.tab"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "secret-colombia",
   "metadata": {},
   "source": [
    "输出结果分别为分类结果和后验概率。在后面加上了数据的真实分类情况作为比较。\n",
    "\n",
    "下面查看误判情况："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "stock-spyware",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     \n",
       "group  1  2\n",
       "    1 11  1\n",
       "    2  1 22"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(group, pred$class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "interracial-draft",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 2 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>class</th><th scope=col>posterior.1</th><th scope=col>posterior.2</th><th scope=col>LD1</th><th scope=col>data.group</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>9</th><td>2</td><td>0.2119048</td><td>0.7880952</td><td>-0.1816424</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>29</th><td>1</td><td>0.7578938</td><td>0.2421062</td><td>-1.0868825</td><td>2</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 2 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & class & posterior.1 & posterior.2 & LD1 & data.group\\\\\n",
       "  & <fct> & <dbl> & <dbl> & <dbl> & <int>\\\\\n",
       "\\hline\n",
       "\t9 & 2 & 0.2119048 & 0.7880952 & -0.1816424 & 1\\\\\n",
       "\t29 & 1 & 0.7578938 & 0.2421062 & -1.0868825 & 2\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 2 × 5\n",
       "\n",
       "| <!--/--> | class &lt;fct&gt; | posterior.1 &lt;dbl&gt; | posterior.2 &lt;dbl&gt; | LD1 &lt;dbl&gt; | data.group &lt;int&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 9 | 2 | 0.2119048 | 0.7880952 | -0.1816424 | 1 |\n",
       "| 29 | 1 | 0.7578938 | 0.2421062 | -1.0868825 | 2 |\n",
       "\n"
      ],
      "text/plain": [
       "   class posterior.1 posterior.2 LD1        data.group\n",
       "9  2     0.2119048   0.7880952   -0.1816424 1         \n",
       "29 1     0.7578938   0.2421062   -1.0868825 2         "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bad <- pred.tab[pred$class != data$group,]; bad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "anticipated-prime",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.0571428571428571"
      ],
      "text/latex": [
       "0.0571428571428571"
      ],
      "text/markdown": [
       "0.0571428571428571"
      ],
      "text/plain": [
       "[1] 0.05714286"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "length(bad$class) / length(pred$class)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "amino-laundry",
   "metadata": {},
   "source": [
    "有两个样本误判：\n",
    "\n",
    "- 9 号样本误判到 $G_2$；\n",
    "- 29 号样本误判到 $G_1$.\n",
    "\n",
    "误判率为: $5.71\\%$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dated-research",
   "metadata": {},
   "source": [
    "交叉确认误判率 (ref https://zhuanlan.zhihu.com/p/23965433 )："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "juvenile-stereo",
   "metadata": {},
   "outputs": [],
   "source": [
    "ll <- lda(group ~ ., data=data, CV=TRUE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "split-editing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     \n",
       "group  1  2\n",
       "    1  8  4\n",
       "    2  2 21"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(group, ll$class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "moral-information",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 6 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>class</th><th scope=col>posterior.1</th><th scope=col>posterior.2</th><th scope=col>LD1</th><th scope=col>data.group</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>5</th><td>1</td><td>0.6533235</td><td>0.3466765</td><td>-0.8997285</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>9</th><td>2</td><td>0.2119048</td><td>0.7880952</td><td>-0.1816424</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>11</th><td>1</td><td>0.6906278</td><td>0.3093722</td><td>-0.9621920</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>12</th><td>1</td><td>0.6832010</td><td>0.3167990</td><td>-0.9494561</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>29</th><td>1</td><td>0.7578938</td><td>0.2421062</td><td>-1.0868825</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>35</th><td>2</td><td>0.1036058</td><td>0.8963942</td><td> 0.1297254</td><td>2</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 6 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & class & posterior.1 & posterior.2 & LD1 & data.group\\\\\n",
       "  & <fct> & <dbl> & <dbl> & <dbl> & <int>\\\\\n",
       "\\hline\n",
       "\t5 & 1 & 0.6533235 & 0.3466765 & -0.8997285 & 1\\\\\n",
       "\t9 & 2 & 0.2119048 & 0.7880952 & -0.1816424 & 1\\\\\n",
       "\t11 & 1 & 0.6906278 & 0.3093722 & -0.9621920 & 1\\\\\n",
       "\t12 & 1 & 0.6832010 & 0.3167990 & -0.9494561 & 1\\\\\n",
       "\t29 & 1 & 0.7578938 & 0.2421062 & -1.0868825 & 2\\\\\n",
       "\t35 & 2 & 0.1036058 & 0.8963942 &  0.1297254 & 2\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 6 × 5\n",
       "\n",
       "| <!--/--> | class &lt;fct&gt; | posterior.1 &lt;dbl&gt; | posterior.2 &lt;dbl&gt; | LD1 &lt;dbl&gt; | data.group &lt;int&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 5 | 1 | 0.6533235 | 0.3466765 | -0.8997285 | 1 |\n",
       "| 9 | 2 | 0.2119048 | 0.7880952 | -0.1816424 | 1 |\n",
       "| 11 | 1 | 0.6906278 | 0.3093722 | -0.9621920 | 1 |\n",
       "| 12 | 1 | 0.6832010 | 0.3167990 | -0.9494561 | 1 |\n",
       "| 29 | 1 | 0.7578938 | 0.2421062 | -1.0868825 | 2 |\n",
       "| 35 | 2 | 0.1036058 | 0.8963942 |  0.1297254 | 2 |\n",
       "\n"
      ],
      "text/plain": [
       "   class posterior.1 posterior.2 LD1        data.group\n",
       "5  1     0.6533235   0.3466765   -0.8997285 1         \n",
       "9  2     0.2119048   0.7880952   -0.1816424 1         \n",
       "11 1     0.6906278   0.3093722   -0.9621920 1         \n",
       "12 1     0.6832010   0.3167990   -0.9494561 1         \n",
       "29 1     0.7578938   0.2421062   -1.0868825 2         \n",
       "35 2     0.1036058   0.8963942    0.1297254 2         "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cv.bad <- pred.tab[ll$class != data$group,]; cv.bad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "stuffed-hacker",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.171428571428571"
      ],
      "text/latex": [
       "0.171428571428571"
      ],
      "text/markdown": [
       "0.171428571428571"
      ],
      "text/plain": [
       "[1] 0.1714286"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "length(cv.bad$class) / length(pred$class)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "typical-culture",
   "metadata": {},
   "source": [
    "共有 6 个样本误判，误判率 $17.14\\%$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "occupational-image",
   "metadata": {},
   "source": [
    "## 先验概率相等"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "viral-fantasy",
   "metadata": {},
   "source": [
    "假设 $\\Sigma_1=\\Sigma_2$， 先验概率按相同的条件下，还是用 lda 函数来做，需要多传一个参数指定先验概率。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "endless-medicine",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Call:\n",
      "lda(group ~ ., data = data, prior = c(1, 1)/2)\n",
      "\n",
      "Prior probabilities of groups:\n",
      "  1   2 \n",
      "0.5 0.5 \n",
      "\n",
      "Group means:\n",
      "        x1       x2       x3      x4        x5        x6       x7\n",
      "1 7.358333 73.66667 1.458333 6.00000 15.250000 0.1716667 49.50000\n",
      "2 7.686957 67.43478 2.043478 5.23913  6.347826 0.2156522 70.34783\n",
      "\n",
      "Coefficients of linear discriminants:\n",
      "             LD1\n",
      "x1 -1.747293e-01\n",
      "x2  1.416021e-05\n",
      "x3  2.072826e-01\n",
      "x4 -2.052228e-01\n",
      "x5 -1.935168e-01\n",
      "x6  8.395266e+00\n",
      "x7  1.917129e-02\n"
     ]
    }
   ],
   "source": [
    "le <- lda(group ~ ., data = data, prior = c(1, 1) / 2)\n",
    "print(le)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "classified-desperate",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "W = -0.17472934*x_1 + 1.416e-05*x_2 + 0.20728259*x_3 + -0.20522277*x_4 + -0.1935168*x_5 + 8.395266*x_6 + 0.01917129*x_7 +"
     ]
    }
   ],
   "source": [
    "# 输出线性判别函数\n",
    "cat(\"W =\", paste(round(le$scaling, 8), \"*x_\", 1:7, \" +\", sep=\"\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "complete-outreach",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 35 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>class</th><th scope=col>posterior.1</th><th scope=col>posterior.2</th><th scope=col>LD1</th><th scope=col>data.group</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>1</th><td>1</td><td>0.6859662059</td><td>3.140338e-01</td><td>-0.9541788</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>2</th><td>1</td><td>0.9807303897</td><td>1.926961e-02</td><td>-2.1152796</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>3</th><td>1</td><td>0.8665260167</td><td>1.334740e-01</td><td>-1.3558819</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>4</th><td>1</td><td>0.9934324560</td><td>6.567544e-03</td><td>-2.5169826</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>5</th><td>1</td><td>0.6533234780</td><td>3.466765e-01</td><td>-0.8997285</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>6</th><td>1</td><td>0.9998065233</td><td>1.934767e-04</td><td>-3.8192153</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>7</th><td>1</td><td>0.9517049526</td><td>4.829505e-02</td><td>-1.7653599</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>8</th><td>1</td><td>0.9172384730</td><td>8.276153e-02</td><td>-1.5531155</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>9</th><td>2</td><td>0.2119047770</td><td>7.880952e-01</td><td>-0.1816424</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>10</th><td>1</td><td>0.9999488458</td><td>5.115424e-05</td><td>-4.3098682</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>11</th><td>1</td><td>0.6906278386</td><td>3.093722e-01</td><td>-0.9621920</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>12</th><td>1</td><td>0.6832009800</td><td>3.167990e-01</td><td>-0.9494561</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>13</th><td>2</td><td>0.0003137630</td><td>9.996862e-01</td><td> 2.3087978</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>14</th><td>2</td><td>0.0371741982</td><td>9.628258e-01</td><td> 0.5340885</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>15</th><td>2</td><td>0.0018008368</td><td>9.981992e-01</td><td> 1.6638437</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>16</th><td>2</td><td>0.0258888529</td><td>9.741111e-01</td><td> 0.6718136</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>17</th><td>2</td><td>0.0233176914</td><td>9.766823e-01</td><td> 0.7113608</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>18</th><td>2</td><td>0.0039412644</td><td>9.960587e-01</td><td> 1.3742004</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>19</th><td>2</td><td>0.0044431240</td><td>9.955569e-01</td><td> 1.3298132</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>20</th><td>2</td><td>0.0724237508</td><td>9.275762e-01</td><td> 0.2743832</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>21</th><td>2</td><td>0.0121654340</td><td>9.878346e-01</td><td> 0.9554851</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>22</th><td>2</td><td>0.0511593102</td><td>9.488407e-01</td><td> 0.4109283</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>23</th><td>2</td><td>0.0011189212</td><td>9.988811e-01</td><td> 1.8395958</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>24</th><td>2</td><td>0.0167026449</td><td>9.832974e-01</td><td> 0.8368939</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>25</th><td>2</td><td>0.0479575131</td><td>9.520425e-01</td><td> 0.4360049</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>26</th><td>2</td><td>0.0155093991</td><td>9.844906e-01</td><td> 0.8646758</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>27</th><td>2</td><td>0.0430578686</td><td>9.569421e-01</td><td> 0.4776422</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>28</th><td>2</td><td>0.3223405288</td><td>6.776595e-01</td><td>-0.3920147</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>29</th><td>1</td><td>0.7578938074</td><td>2.421062e-01</td><td>-1.0868825</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>30</th><td>2</td><td>0.0006240626</td><td>9.993759e-01</td><td> 2.0551013</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>31</th><td>2</td><td>0.0001531707</td><td>9.998468e-01</td><td> 2.5733078</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>32</th><td>2</td><td>0.0068117313</td><td>9.931883e-01</td><td> 1.1713567</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>33</th><td>2</td><td>0.0018353157</td><td>9.981647e-01</td><td> 1.6568369</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>34</th><td>2</td><td>0.0324563440</td><td>9.675437e-01</td><td> 0.5859424</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>35</th><td>2</td><td>0.1036058164</td><td>8.963942e-01</td><td> 0.1297254</td><td>2</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 35 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & class & posterior.1 & posterior.2 & LD1 & data.group\\\\\n",
       "  & <fct> & <dbl> & <dbl> & <dbl> & <int>\\\\\n",
       "\\hline\n",
       "\t1 & 1 & 0.6859662059 & 3.140338e-01 & -0.9541788 & 1\\\\\n",
       "\t2 & 1 & 0.9807303897 & 1.926961e-02 & -2.1152796 & 1\\\\\n",
       "\t3 & 1 & 0.8665260167 & 1.334740e-01 & -1.3558819 & 1\\\\\n",
       "\t4 & 1 & 0.9934324560 & 6.567544e-03 & -2.5169826 & 1\\\\\n",
       "\t5 & 1 & 0.6533234780 & 3.466765e-01 & -0.8997285 & 1\\\\\n",
       "\t6 & 1 & 0.9998065233 & 1.934767e-04 & -3.8192153 & 1\\\\\n",
       "\t7 & 1 & 0.9517049526 & 4.829505e-02 & -1.7653599 & 1\\\\\n",
       "\t8 & 1 & 0.9172384730 & 8.276153e-02 & -1.5531155 & 1\\\\\n",
       "\t9 & 2 & 0.2119047770 & 7.880952e-01 & -0.1816424 & 1\\\\\n",
       "\t10 & 1 & 0.9999488458 & 5.115424e-05 & -4.3098682 & 1\\\\\n",
       "\t11 & 1 & 0.6906278386 & 3.093722e-01 & -0.9621920 & 1\\\\\n",
       "\t12 & 1 & 0.6832009800 & 3.167990e-01 & -0.9494561 & 1\\\\\n",
       "\t13 & 2 & 0.0003137630 & 9.996862e-01 &  2.3087978 & 2\\\\\n",
       "\t14 & 2 & 0.0371741982 & 9.628258e-01 &  0.5340885 & 2\\\\\n",
       "\t15 & 2 & 0.0018008368 & 9.981992e-01 &  1.6638437 & 2\\\\\n",
       "\t16 & 2 & 0.0258888529 & 9.741111e-01 &  0.6718136 & 2\\\\\n",
       "\t17 & 2 & 0.0233176914 & 9.766823e-01 &  0.7113608 & 2\\\\\n",
       "\t18 & 2 & 0.0039412644 & 9.960587e-01 &  1.3742004 & 2\\\\\n",
       "\t19 & 2 & 0.0044431240 & 9.955569e-01 &  1.3298132 & 2\\\\\n",
       "\t20 & 2 & 0.0724237508 & 9.275762e-01 &  0.2743832 & 2\\\\\n",
       "\t21 & 2 & 0.0121654340 & 9.878346e-01 &  0.9554851 & 2\\\\\n",
       "\t22 & 2 & 0.0511593102 & 9.488407e-01 &  0.4109283 & 2\\\\\n",
       "\t23 & 2 & 0.0011189212 & 9.988811e-01 &  1.8395958 & 2\\\\\n",
       "\t24 & 2 & 0.0167026449 & 9.832974e-01 &  0.8368939 & 2\\\\\n",
       "\t25 & 2 & 0.0479575131 & 9.520425e-01 &  0.4360049 & 2\\\\\n",
       "\t26 & 2 & 0.0155093991 & 9.844906e-01 &  0.8646758 & 2\\\\\n",
       "\t27 & 2 & 0.0430578686 & 9.569421e-01 &  0.4776422 & 2\\\\\n",
       "\t28 & 2 & 0.3223405288 & 6.776595e-01 & -0.3920147 & 2\\\\\n",
       "\t29 & 1 & 0.7578938074 & 2.421062e-01 & -1.0868825 & 2\\\\\n",
       "\t30 & 2 & 0.0006240626 & 9.993759e-01 &  2.0551013 & 2\\\\\n",
       "\t31 & 2 & 0.0001531707 & 9.998468e-01 &  2.5733078 & 2\\\\\n",
       "\t32 & 2 & 0.0068117313 & 9.931883e-01 &  1.1713567 & 2\\\\\n",
       "\t33 & 2 & 0.0018353157 & 9.981647e-01 &  1.6568369 & 2\\\\\n",
       "\t34 & 2 & 0.0324563440 & 9.675437e-01 &  0.5859424 & 2\\\\\n",
       "\t35 & 2 & 0.1036058164 & 8.963942e-01 &  0.1297254 & 2\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 35 × 5\n",
       "\n",
       "| <!--/--> | class &lt;fct&gt; | posterior.1 &lt;dbl&gt; | posterior.2 &lt;dbl&gt; | LD1 &lt;dbl&gt; | data.group &lt;int&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 1 | 1 | 0.6859662059 | 3.140338e-01 | -0.9541788 | 1 |\n",
       "| 2 | 1 | 0.9807303897 | 1.926961e-02 | -2.1152796 | 1 |\n",
       "| 3 | 1 | 0.8665260167 | 1.334740e-01 | -1.3558819 | 1 |\n",
       "| 4 | 1 | 0.9934324560 | 6.567544e-03 | -2.5169826 | 1 |\n",
       "| 5 | 1 | 0.6533234780 | 3.466765e-01 | -0.8997285 | 1 |\n",
       "| 6 | 1 | 0.9998065233 | 1.934767e-04 | -3.8192153 | 1 |\n",
       "| 7 | 1 | 0.9517049526 | 4.829505e-02 | -1.7653599 | 1 |\n",
       "| 8 | 1 | 0.9172384730 | 8.276153e-02 | -1.5531155 | 1 |\n",
       "| 9 | 2 | 0.2119047770 | 7.880952e-01 | -0.1816424 | 1 |\n",
       "| 10 | 1 | 0.9999488458 | 5.115424e-05 | -4.3098682 | 1 |\n",
       "| 11 | 1 | 0.6906278386 | 3.093722e-01 | -0.9621920 | 1 |\n",
       "| 12 | 1 | 0.6832009800 | 3.167990e-01 | -0.9494561 | 1 |\n",
       "| 13 | 2 | 0.0003137630 | 9.996862e-01 |  2.3087978 | 2 |\n",
       "| 14 | 2 | 0.0371741982 | 9.628258e-01 |  0.5340885 | 2 |\n",
       "| 15 | 2 | 0.0018008368 | 9.981992e-01 |  1.6638437 | 2 |\n",
       "| 16 | 2 | 0.0258888529 | 9.741111e-01 |  0.6718136 | 2 |\n",
       "| 17 | 2 | 0.0233176914 | 9.766823e-01 |  0.7113608 | 2 |\n",
       "| 18 | 2 | 0.0039412644 | 9.960587e-01 |  1.3742004 | 2 |\n",
       "| 19 | 2 | 0.0044431240 | 9.955569e-01 |  1.3298132 | 2 |\n",
       "| 20 | 2 | 0.0724237508 | 9.275762e-01 |  0.2743832 | 2 |\n",
       "| 21 | 2 | 0.0121654340 | 9.878346e-01 |  0.9554851 | 2 |\n",
       "| 22 | 2 | 0.0511593102 | 9.488407e-01 |  0.4109283 | 2 |\n",
       "| 23 | 2 | 0.0011189212 | 9.988811e-01 |  1.8395958 | 2 |\n",
       "| 24 | 2 | 0.0167026449 | 9.832974e-01 |  0.8368939 | 2 |\n",
       "| 25 | 2 | 0.0479575131 | 9.520425e-01 |  0.4360049 | 2 |\n",
       "| 26 | 2 | 0.0155093991 | 9.844906e-01 |  0.8646758 | 2 |\n",
       "| 27 | 2 | 0.0430578686 | 9.569421e-01 |  0.4776422 | 2 |\n",
       "| 28 | 2 | 0.3223405288 | 6.776595e-01 | -0.3920147 | 2 |\n",
       "| 29 | 1 | 0.7578938074 | 2.421062e-01 | -1.0868825 | 2 |\n",
       "| 30 | 2 | 0.0006240626 | 9.993759e-01 |  2.0551013 | 2 |\n",
       "| 31 | 2 | 0.0001531707 | 9.998468e-01 |  2.5733078 | 2 |\n",
       "| 32 | 2 | 0.0068117313 | 9.931883e-01 |  1.1713567 | 2 |\n",
       "| 33 | 2 | 0.0018353157 | 9.981647e-01 |  1.6568369 | 2 |\n",
       "| 34 | 2 | 0.0324563440 | 9.675437e-01 |  0.5859424 | 2 |\n",
       "| 35 | 2 | 0.1036058164 | 8.963942e-01 |  0.1297254 | 2 |\n",
       "\n"
      ],
      "text/plain": [
       "   class posterior.1  posterior.2  LD1        data.group\n",
       "1  1     0.6859662059 3.140338e-01 -0.9541788 1         \n",
       "2  1     0.9807303897 1.926961e-02 -2.1152796 1         \n",
       "3  1     0.8665260167 1.334740e-01 -1.3558819 1         \n",
       "4  1     0.9934324560 6.567544e-03 -2.5169826 1         \n",
       "5  1     0.6533234780 3.466765e-01 -0.8997285 1         \n",
       "6  1     0.9998065233 1.934767e-04 -3.8192153 1         \n",
       "7  1     0.9517049526 4.829505e-02 -1.7653599 1         \n",
       "8  1     0.9172384730 8.276153e-02 -1.5531155 1         \n",
       "9  2     0.2119047770 7.880952e-01 -0.1816424 1         \n",
       "10 1     0.9999488458 5.115424e-05 -4.3098682 1         \n",
       "11 1     0.6906278386 3.093722e-01 -0.9621920 1         \n",
       "12 1     0.6832009800 3.167990e-01 -0.9494561 1         \n",
       "13 2     0.0003137630 9.996862e-01  2.3087978 2         \n",
       "14 2     0.0371741982 9.628258e-01  0.5340885 2         \n",
       "15 2     0.0018008368 9.981992e-01  1.6638437 2         \n",
       "16 2     0.0258888529 9.741111e-01  0.6718136 2         \n",
       "17 2     0.0233176914 9.766823e-01  0.7113608 2         \n",
       "18 2     0.0039412644 9.960587e-01  1.3742004 2         \n",
       "19 2     0.0044431240 9.955569e-01  1.3298132 2         \n",
       "20 2     0.0724237508 9.275762e-01  0.2743832 2         \n",
       "21 2     0.0121654340 9.878346e-01  0.9554851 2         \n",
       "22 2     0.0511593102 9.488407e-01  0.4109283 2         \n",
       "23 2     0.0011189212 9.988811e-01  1.8395958 2         \n",
       "24 2     0.0167026449 9.832974e-01  0.8368939 2         \n",
       "25 2     0.0479575131 9.520425e-01  0.4360049 2         \n",
       "26 2     0.0155093991 9.844906e-01  0.8646758 2         \n",
       "27 2     0.0430578686 9.569421e-01  0.4776422 2         \n",
       "28 2     0.3223405288 6.776595e-01 -0.3920147 2         \n",
       "29 1     0.7578938074 2.421062e-01 -1.0868825 2         \n",
       "30 2     0.0006240626 9.993759e-01  2.0551013 2         \n",
       "31 2     0.0001531707 9.998468e-01  2.5733078 2         \n",
       "32 2     0.0068117313 9.931883e-01  1.1713567 2         \n",
       "33 2     0.0018353157 9.981647e-01  1.6568369 2         \n",
       "34 2     0.0324563440 9.675437e-01  0.5859424 2         \n",
       "35 2     0.1036058164 8.963942e-01  0.1297254 2         "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pred_e <- predict(le)\n",
    "pred_e.tab <- data.frame(pred_e, data.group=group); pred.tab"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "independent-platinum",
   "metadata": {},
   "source": [
    "误判情况："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "recognized-durham",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     \n",
       "group  1  2\n",
       "    1 11  1\n",
       "    2  1 22"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "table(group, pred_e$class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "mechanical-baghdad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 2 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>class</th><th scope=col>posterior.1</th><th scope=col>posterior.2</th><th scope=col>LD1</th><th scope=col>data.group</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>9</th><td>2</td><td>0.3400897</td><td>0.6599103</td><td> 0.2444662</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>29</th><td>1</td><td>0.8571422</td><td>0.1428578</td><td>-0.6607739</td><td>2</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 2 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & class & posterior.1 & posterior.2 & LD1 & data.group\\\\\n",
       "  & <fct> & <dbl> & <dbl> & <dbl> & <int>\\\\\n",
       "\\hline\n",
       "\t9 & 2 & 0.3400897 & 0.6599103 &  0.2444662 & 1\\\\\n",
       "\t29 & 1 & 0.8571422 & 0.1428578 & -0.6607739 & 2\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 2 × 5\n",
       "\n",
       "| <!--/--> | class &lt;fct&gt; | posterior.1 &lt;dbl&gt; | posterior.2 &lt;dbl&gt; | LD1 &lt;dbl&gt; | data.group &lt;int&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 9 | 2 | 0.3400897 | 0.6599103 |  0.2444662 | 1 |\n",
       "| 29 | 1 | 0.8571422 | 0.1428578 | -0.6607739 | 2 |\n",
       "\n"
      ],
      "text/plain": [
       "   class posterior.1 posterior.2 LD1        data.group\n",
       "9  2     0.3400897   0.6599103    0.2444662 1         \n",
       "29 1     0.8571422   0.1428578   -0.6607739 2         "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bad_e <- pred_e.tab[pred_e$class != data$group,]; bad_e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "natural-wisconsin",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.0571428571428571"
      ],
      "text/latex": [
       "0.0571428571428571"
      ],
      "text/markdown": [
       "0.0571428571428571"
      ],
      "text/plain": [
       "[1] 0.05714286"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 误判率\n",
    "length(bad_e$class) / length(pred_e$class)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "concerned-presence",
   "metadata": {},
   "source": [
    "交叉确认："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "logical-healing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     \n",
       "group  1  2\n",
       "    1 11  1\n",
       "    2  3 20"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lel <- lda(group ~ ., data=data,  prior = c(1, 1) / 2, CV=TRUE)\n",
    "table(group, lel$class)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "freelance-story",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"dataframe\">\n",
       "<caption>A data.frame: 4 × 5</caption>\n",
       "<thead>\n",
       "\t<tr><th></th><th scope=col>class</th><th scope=col>posterior.1</th><th scope=col>posterior.2</th><th scope=col>LD1</th><th scope=col>data.group</th></tr>\n",
       "\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "\t<tr><th scope=row>9</th><td>2</td><td>0.3400897</td><td>0.6599103</td><td> 0.24446615</td><td>1</td></tr>\n",
       "\t<tr><th scope=row>28</th><td>2</td><td>0.4769042</td><td>0.5230958</td><td> 0.03409383</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>29</th><td>1</td><td>0.8571422</td><td>0.1428578</td><td>-0.66077393</td><td>2</td></tr>\n",
       "\t<tr><th scope=row>35</th><td>2</td><td>0.1813542</td><td>0.8186458</td><td> 0.55583396</td><td>2</td></tr>\n",
       "</tbody>\n",
       "</table>\n"
      ],
      "text/latex": [
       "A data.frame: 4 × 5\n",
       "\\begin{tabular}{r|lllll}\n",
       "  & class & posterior.1 & posterior.2 & LD1 & data.group\\\\\n",
       "  & <fct> & <dbl> & <dbl> & <dbl> & <int>\\\\\n",
       "\\hline\n",
       "\t9 & 2 & 0.3400897 & 0.6599103 &  0.24446615 & 1\\\\\n",
       "\t28 & 2 & 0.4769042 & 0.5230958 &  0.03409383 & 2\\\\\n",
       "\t29 & 1 & 0.8571422 & 0.1428578 & -0.66077393 & 2\\\\\n",
       "\t35 & 2 & 0.1813542 & 0.8186458 &  0.55583396 & 2\\\\\n",
       "\\end{tabular}\n"
      ],
      "text/markdown": [
       "\n",
       "A data.frame: 4 × 5\n",
       "\n",
       "| <!--/--> | class &lt;fct&gt; | posterior.1 &lt;dbl&gt; | posterior.2 &lt;dbl&gt; | LD1 &lt;dbl&gt; | data.group &lt;int&gt; |\n",
       "|---|---|---|---|---|---|\n",
       "| 9 | 2 | 0.3400897 | 0.6599103 |  0.24446615 | 1 |\n",
       "| 28 | 2 | 0.4769042 | 0.5230958 |  0.03409383 | 2 |\n",
       "| 29 | 1 | 0.8571422 | 0.1428578 | -0.66077393 | 2 |\n",
       "| 35 | 2 | 0.1813542 | 0.8186458 |  0.55583396 | 2 |\n",
       "\n"
      ],
      "text/plain": [
       "   class posterior.1 posterior.2 LD1         data.group\n",
       "9  2     0.3400897   0.6599103    0.24446615 1         \n",
       "28 2     0.4769042   0.5230958    0.03409383 2         \n",
       "29 1     0.8571422   0.1428578   -0.66077393 2         \n",
       "35 2     0.1813542   0.8186458    0.55583396 2         "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "cv.bad_e <- pred_e.tab[lel$class != data$group,]; cv.bad_e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "satisfied-buffalo",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "0.114285714285714"
      ],
      "text/latex": [
       "0.114285714285714"
      ],
      "text/markdown": [
       "0.114285714285714"
      ],
      "text/plain": [
       "[1] 0.1142857"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 误判率\n",
    "length(cv.bad_e$class) / length(pred_e$class)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "superb-ridge",
   "metadata": {},
   "source": [
    "这里得到了误判率 $11.43\\%$."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.0.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
